﻿Imports System.Data.Linq
Imports System.Linq

Public Class MFileExchange
    Private db As New TanNamChinhDataContext

    Public Sub InsertNewRecord(ByVal obj As tblFileExchange)
        'db.tblCurrencies.InsertOnSubmit(obj)
        db.GetTable(Of tblFileExchange)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub
    Public Function GetFileExchange(ByVal idFile As Integer) As ISingleResult(Of sp_Quyen_GetBuyingCurrListResult)
        'Try
        '    Dim q = (From a In db.tblFileFees Join b In db.tblCurrencies On b.ID_Currency Equals a.ID_Currency
        '             Join c In db.tblFileExchanges On c.ID_File Equals a.ID_File
        '            Where a.ID_File = idFile And Not b.Code = "VND"
        '            Select New With {b.ID_Currency, b.Code, c.Ratio}).Distinct()
        '    'select distinct c.ID_Currency, c.Code, h.Ratio

        '    If q.Count = 0 Then
        '        'Chưa được lưu trong database, cần kiểm tra và tự thêm vào
        '        q = From a In db.tblFileFees
        '             Where a.ID_File = idFile
        '                Select New With {a.ID_Currency, a.tblCurrency.Code, .Ratio = 0}
        '    End If

        '    Return q
        'Catch ex As Exception
        '    Return Nothing
        'End Try
        Try
            Return db.sp_Quyen_GetBuyingCurrList(idFile)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Sub UpdateRecord(ByVal obj As tblFileExchange)
        'Dim getData = (From b In db.tblFiles
        '                        Where b.ID_File = obj.ID_File
        '                        Select b).Single()
        'getData.CreatedDate = obj.CreatedDate
        'getData.ID_Customer = obj.ID_Customer
        'getData.ID_Company = obj.ID_Company
        'getData.ID_ForeignCompany = obj.ID_ForeignCompany
        'getData.ID_Agent = obj.ID_Agent
        'getData.IsExport = obj.IsExport
        'getData.IsD2D = obj.IsD2D
        'getData.IsFCL = obj.IsFCL
        'getData.ID_Unit = obj.ID_Unit

        'db.SubmitChanges()

    End Sub
    Public Sub UpdateRatioBuying(ByVal obj As tblFileExchange)
        Dim getData = (From b In db.tblFileExchanges
                                Where b.ID_File = obj.ID_File And b.ID_Currency = obj.ID_Currency
                                Select b).Single()

        getData.Ratio = obj.Ratio

        db.SubmitChanges()

    End Sub
    
 
    Public Sub DeleteAllRecord(ByVal _idFile As Integer)
        Try
            db.tblFileExchanges.DeleteAllOnSubmit( _
              (From r In db.tblFileExchanges _
               Where r.ID_File = _idFile))
            db.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function GetRatioCurrency(ByVal idFile As Integer, ByVal idCurr As Integer) As Double
        Dim ratios As Double
        Try
            ratios = (From b In db.tblFileExchanges
                     Where b.ID_File = idFile And b.ID_Currency = idCurr
                     Select b.Ratio).First()
        Catch ex As Exception
            Return 0
        End Try
        Return ratios
    End Function
    Public Function GetRatioCurrencyFollowHBL(ByVal idFile As Integer, ByVal idHBL As Integer) As Double
        Dim ratios As Double
        Try
            ratios = (From b In db.tblHBLs
                     Where b.tblFileMBL.ID_File = idFile And b.ID_HBL = idHBL
                     Select b.Ratio).First()
        Catch ex As Exception
            Return 0
        End Try
        Return ratios
    End Function

    Public Function IsCurrencyExist(ByVal idFile As Integer, ByVal idCurr As Integer) As Boolean
        'Kiểm tra nếu có rồi thì trả về true
        Dim q = From a In db.tblFileExchanges
                Where a.ID_File = idFile And a.ID_Currency = idCurr
                Select a


        If q.Count > 0 Then
            Return True
        Else
            Return False
        End If
        Return False

    End Function
End Class
